<template>
  <div>
    <!-- Header 区域 -->
    <van-nav-bar fixed title="文章详情" left-arrow @click-left="$router.back()" />
    <!-- 文章加载显示 -->
    <van-loading color="#1989fa" vertical v-if="isLoading" class="loading">
      正在疯狂加载文章...
    </van-loading>

    <!-- 文章信息区域 -->
    <div class="article-container" v-else>
      <!-- 文章标题 -->
      <h1 class="art-title">{{articleDeatil.title}}</h1>

      <!-- 用户信息 -->
      <van-cell center :title="articleDeatil.aut_name" :label="formatTime(articleDeatil.pubdate)">
        <template #icon>
          <img :src="articleDeatil.aut_photo" alt="头像" class="avatar">
        </template>
        <template #default>
          <div>
            <van-button type="info" size="mini" v-if="articleDeatil.is_followed" @click="followAuth(false)">已关注</van-button>
            <van-button icon="plus" type="info" v-else size="mini" plain @click="followAuth(true)">关注</van-button>
          </div>
        </template>
      </van-cell>

      <!-- 分割线 -->
      <van-divider></van-divider>

      <!-- 文章内容 -->
      <div class="art-content" v-html="articleDeatil.content"></div>

      <!-- 分割线 -->
      <van-divider>End</van-divider>

      <!-- 点赞 -->
      <div class="like-box">
        <van-button icon="good-job" type="danger" size="small" v-if="articleDeatil.attitude === 1" @click="likeAtrilce(false)">已点赞</van-button>
        <van-button icon="good-job-o" type="danger" plain size="small" v-else @click="likeAtrilce(true)">点赞</van-button>
      </div>
    </div>
    <!-- 分割线 -->
      <van-divider></van-divider>
    <CommentList />
  </div>
</template>

<script>
import { articleDetailAPI, followedUserAPI, unFollowedUserAPI, articleLikeAPI, articleDisLikeAPI } from '@/api/index'
import { timeAgo } from '@/utils/date'
import CommentList from './components/CommentList.vue'
export default {
  name: 'ArticleDetail',
  components: { CommentList },
  props: {
    aid: {
      type: String
    }
  },
  data () {
    return {
      articleDeatil: {},
      isLoading: false
    }
  },
  methods: {
    // 获取文章详情
    async getArticleDeatil () {
      // 文章加载中
      this.isLoading = true
      const res = await articleDetailAPI({
        id: this.aid
      })
      this.articleDeatil = res.data.data
      // 加载完成
      this.isLoading = false
    },
    // 关注作者
    async followAuth (bool) {
      if (bool) {
        // 关注作者
        this.articleDeatil.is_followed = true
        await followedUserAPI({
          target: this.articleDeatil.aut_id
        })
      } else {
        // 取消关注
        this.articleDeatil.is_followed = false
        await unFollowedUserAPI({
          uid: this.articleDeatil.aut_id
        })
      }
    },
    // 点赞文章
    async likeAtrilce (bool) {
      if (bool) {
        // 点赞
        this.articleDeatil.attitude = 1
        await articleLikeAPI({
          target: this.aid
        })
      } else {
        // 取消点赞
        this.articleDeatil.attitude = -1
        await articleDisLikeAPI({
          artId: this.aid
        })
      }
    },
    // 格式化时间
    formatTime: timeAgo,

    // 跳转到评论区
    moveFn () {
      document.querySelector('.like-box').scrollIntoView({
        behavior: 'smooth'
      })
    }
  },

  created () {
    this.getArticleDeatil()
  },
  mounted () {
    this.$bus.$on('move', this.moveFn)
  },
  beforeDestroy () {
    this.$bus.$off('move')
  }
}
</script>

<style scoped lang="less">
.article-container {
  padding: 10px;
  margin-top: 46px;
}
.art-title {
  font-size: 16px;
  font-weight: bold;
  margin: 10px 0;
}

.art-content {
  font-size: 12px;
  line-height: 24px;
  width: 100%;
  overflow-x: scroll;
  word-break: break-all;
    /deep/ img{
    width: 100%;
  }
    /deep/ pre {
        white-space: pre-wrap;
        word-wrap: break-word;
    }
}

.van-cell {
  padding: 5px 0;
  &::after {
    display: none;
  }
}

.avatar {
  width: 60px;
  height: 60px;
  border-radius: 50%;
  background-color: #f8f8f8;
  margin-right: 5px;
  border: none;
}

.like-box {
  display: flex;
  justify-content: center;
}
.loading {
  margin-top: 50px;
  text-align: center;
}
</style>
